﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for AuthorService
/// </summary>
/// 
namespace FacebookApp
{
    public class AuthorService
    {

        //readonly static DataClassesDataContext dt = new DataClassesDataContext();

        #region[Author_GetbyAll]
        public static List<Author> Author_GetByAll()
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    var da = (from au in dt.Authors select au).ToList();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        #endregion

        #region[Author_GetbyId]
        public static Author Author_GetById(string Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    var da = (from au in dt.Authors where au.Id == id select au).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region[Author_GetbyTop]
        public static List<Author> Author_GetByTop(string Top, string Where, string Order)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    string query = "Select * from [Author]";
                    if (!Top.Equals(""))
                        query = " Select top " + Top + " * from [Author] ";
                    if (!Where.Equals(""))
                        query += " where " + Where + " ";
                    if (!Order.Equals(""))
                        query += " order by " + Order;

                    IEnumerable<Author> results = dt.ExecuteQuery<Author>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
        #endregion

        #region[Author_Insert]
        public static void Author_Insert(AuthorInfo Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    Author new_author = new Author();
                    new_author.Name = Data.Name;
                    new_author.Decription = Data.Decription;
                    dt.Authors.InsertOnSubmit(new_author);
                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }

        }
        #endregion

        #region[Author_Update]
        public static void Author_Update(AuthorInfo Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data.Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Author da = dt.Authors.Single(au => au.Id == id);
                    da.Name = Data.Name;
                    da.Decription = Data.Decription;

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion

        #region[Author_Delete]

        public static void Author_Delete(string Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Author da = dt.Authors.Single(au => au.Id == id);
                    dt.Authors.DeleteOnSubmit(da);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion
    }


    public class AuthorInfo
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Decription { get; set; }

    }
}